package com.securitytrace.client.rolelistener;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.securitytrace.client.ManagerClient;
import com.securitytrace.dataModel.RoleDataModel;
import com.securitytrace.init.Init;
import com.securitytrace.listener.AddRoleButtonListener;
import com.securitytrace.listener.UpdateRoleButtonListener;
import com.securitytrace.model.Role;
import com.securitytrace.popupwindow.RoleWindow;

public class RoleActionListener {
	
	private ManagerClient managerClient;

	public void addRole() {
		RoleWindow roleFrame = new RoleWindow(managerClient, true);
		AddRoleButtonListener listener = new AddRoleButtonListener(roleFrame);
		roleFrame.setRoleSubmitButton(listener);
		roleFrame.setVisible(true);
	}
	
	public void updateRole() {
		int row = managerClient.getRolePanel().getRoleInfoTable().getSelectedRow();
		if(row >= 0) {
			Role role = managerClient.getRoleModel().getRow(row);
			RoleWindow roleFrame = new RoleWindow(managerClient,true);
			UpdateRoleButtonListener listener = new UpdateRoleButtonListener(roleFrame);
			listener.setRole(role);
			roleFrame.setRoleSubmitButton(listener);
			roleFrame.getRoleTextField().setText(role.getName());
			roleFrame.getRoleTextArea().setText(role.getDescription());
			roleFrame.setVisible(true);
			if(null != managerClient.getResultRole()) {
				managerClient.getRoleModel().setValueAtRow(managerClient.getResultRole(), row);
			}
		}
	}
	
	public void deleteRole() {
		int row = managerClient.getRolePanel().getRoleInfoTable().getSelectedRow();
		if(row >= 0) {
			RoleDataModel roleDataModel = managerClient.getRoleModel();
			Role role = roleDataModel.getRow(row);
			
			int currentPage = roleDataModel.getRoleCurrentPage();
			int pageSize = managerClient.getPageSize();
			int totalPage = 1;
			
			try {
				Init.getRoleService().deleteById(role.getId());
				
				List<Role> roles = new ArrayList<Role>();
				roles = Init.getRoleService().getListByPage((currentPage - 1) * pageSize, pageSize);
				if((0 >= roles.size() || null == roles) && currentPage > 1) {
					roles = Init.getRoleService().getListByPage((currentPage - 2) * pageSize, pageSize);
					roleDataModel.setRoleCurrentPage(currentPage - 1);
				}
				
				long roleNumber = Init.getRoleService().getTotalCount();
				if((roleNumber % pageSize) == 0) {
					totalPage = (int) (roleNumber / pageSize);
				}else {
					totalPage = (int) (roleNumber / pageSize) + 1;
				}
				roleDataModel.setRoleTotal(totalPage);
				roleDataModel.setDataVector(roles);
				managerClient.getRolePanel().getRolePageInfoLabel().setText(roleDataModel.getRolePageInfo());
				
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "操作失败");
				return ;
			}
		}
	}
	
	public void homePageAction() {
		if (managerClient.getRoleModel().getRoleCurrentPage() > 1) {
			RoleDataModel model = managerClient.getRoleModel();
			model.setDataVector(Init.getRoleService().getListByPage(0,
					managerClient.getPageSize()));
			model.setRoleCurrentPage(1);
			managerClient.getRolePanel().getRoleInfoTable().setModel(model);
			managerClient.getRolePanel().getRolePageInfoLabel()
					.setText(model.getRolePageInfo());
		}
	}
	
	public void previousPageAction() {
		RoleDataModel model = managerClient.getRoleModel();
		int currentPage = model.getRoleCurrentPage();
		if (currentPage > 1) {
			model.setRoleCurrentPage(currentPage - 1);
			model.setDataVector(Init.getRoleService().getListByPage(
					(currentPage - 2) * managerClient.getPageSize(),
					managerClient.getPageSize()));
			managerClient.getRolePanel().getRoleInfoTable().setModel(model);
			managerClient.getRolePanel().getRolePageInfoLabel()
					.setText(model.getRolePageInfo());
		}
	}
	
	public void nextPageAction() {
		RoleDataModel model = managerClient.getRoleModel();
		int currentPage = model.getRoleCurrentPage();
		int totalPage = model.getRoleTotal();
		if (currentPage < totalPage) {
			model.setRoleCurrentPage(currentPage + 1);
			model.setDataVector(Init.getRoleService().getListByPage(
					currentPage * managerClient.getPageSize(), managerClient.getPageSize()));
			managerClient.getRolePanel().getRoleInfoTable().setModel(model);
			managerClient.getRolePanel().getRolePageInfoLabel()
					.setText(model.getRolePageInfo());
		}
	}
	
	public void lastPageAction() {
		RoleDataModel model = managerClient.getRoleModel();
		int currentPage = model.getRoleCurrentPage();
		int totalPage = model.getRoleTotal();
		if (currentPage < totalPage) {
			model.setRoleCurrentPage(totalPage);
			model.setDataVector(Init.getRoleService().getListByPage(
					(totalPage - 1) * managerClient.getPageSize(),
					managerClient.getPageSize()));
			model.setRoleCurrentPage(totalPage);
			managerClient.getRolePanel().getRoleInfoTable().setModel(model);
			managerClient.getRolePanel().getRolePageInfoLabel()
					.setText(model.getRolePageInfo());
		}
	}
	
	/**以下为get/set方法**/
	public ManagerClient getManagerClient() {
		return managerClient;
	}
	
	public void setManagerClient(ManagerClient managerClient) {
		this.managerClient = managerClient;
	}
}
